package portlet;

import java.io.IOException;

import java.util.StringTokenizer;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.WindowState;

import oracle.adf.model.binding.DCIteratorBinding;

import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewObject;

import oracle.portlet.bridge.adf.application.ADFBridgePortlet;

public class danhmucthutuc extends ADFBridgePortlet {
    private String urlDanhsach;
    private String danhmucid;
    private String langid;
    private String viewRender;
    private String labelDanhmuc;
    
    protected String getTitle(RenderRequest request) {
        // Get the customized title. This defaults to the declared title.
        PortletPreferences prefs = request.getPreferences();
        return prefs.getValue(PORTLETTITLE_KEY, super.getTitle(request));
    }
    public static final String APPLY_ACTION = "apply_action";
    public static final String OK_ACTION = "ok_action";
    public static final String PORTLETTITLE_KEY = "portletTitle";

    private String[] buildValueArray(String values) {
        if (values.indexOf(',') < 0) {
            return new String[] {values};
        }
        StringTokenizer st = new StringTokenizer(values, ",");
        String[] valueArray = new String[st.countTokens()];
        int i = 0;
        while (st.hasMoreTokens()) {
            valueArray[i] = st.nextToken().trim();
            i++;
        }
        return valueArray;
    }

    public void processAction(ActionRequest request,
                              ActionResponse response) throws PortletException,
                                                              IOException {
        // Determine which action.
        String okAction = request.getParameter(OK_ACTION);
        String applyAction = request.getParameter(APPLY_ACTION);

        if (okAction != null || applyAction != null) {
            // Save the preferences.
            PortletPreferences prefs = request.getPreferences();
            String param = request.getParameter(PORTLETTITLE_KEY);
            prefs.setValues(PORTLETTITLE_KEY, buildValueArray(param));
            prefs.store();
            if (okAction != null) {
                response.setPortletMode(PortletMode.VIEW);
                response.setWindowState(WindowState.NORMAL);
            }
        } else {
            request.setAttribute("javax.portlet.config", getPortletConfig());
            super.processAction(request, response);
        }
    }

    public void render(RenderRequest request,
                       RenderResponse response) throws PortletException,
                                                       IOException {
        request.setAttribute("javax.portlet.config", getPortletConfig());
        super.render(request, response);
    }

    public String getViewRender() {
        if (Boolean.FALSE.equals(Utils.getElExpression("#{adfFacesContext.postback}"))) {
            getUrlDanhsach();
            getLangid();
            getDanhmucid();
            getDanhmucByLanguage();
        }
        return viewRender;
    }
    
    public void getDanhmucByLanguage() {
        DCIteratorBinding danhmucIterator =
            Utils.getIteratorBinding("TthcDanhmucView1Iterator");
        ViewObject vo = danhmucIterator.getViewObject();
        ViewCriteria vc =
            vo.getViewCriteriaManager().getViewCriteria("TthcDanhmucViewCriteria");
        vo.ensureVariableManager().setVariableValue("varLang", langid);
        vo.getViewCriteriaManager().removeViewCriteria("TthcDanhmucViewCriteria");
        vo.getViewCriteriaManager().applyViewCriteria(vc);
        vo.setRangeSize(-1);
        vo.executeQuery();
    }

    public String getUrlDanhsach() {
        try {
            urlDanhsach = Utils.getPortletRequest().getParameter("urldanhsach");
        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }
        return urlDanhsach;
    }

    public String getDanhmucid() {
        try {
            danhmucid = Utils.getPortletRequest().getParameter("danhmuc");
        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }
        return danhmucid;
    }

    public String getLangid() {
        langid = Utils.getPortalLocale().getLanguage();
        return langid;
    }

    public String getLabelDanhmuc() {
        labelDanhmuc = Utils.getResource("TIEU_DE_DANH_MUC");
        return labelDanhmuc;
    }
}
